package tr.edu.boun.phys.springmassmodel.UI.PropertiesWindow;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame; 
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tr.edu.boun.phys.springmassmodel.Mass;
import tr.edu.boun.phys.springmassmodel.Spring;
import tr.edu.boun.phys.springmassmodel.SpringMassModel;
import tr.edu.boun.phys.springmassmodel.Vector2D;
import tr.edu.boun.phys.springmassmodel.UI.ResourceManager;

public class PropertiesWindow extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = -4026678923171632792L;
	
	private ResourceManager rm;
	private SpringMassModel model;
	private JPanel panel;
	private JTextField textMassAmount;
	private JTextField textFriction;
	private JTextField textMassNo;
	private JButton buttonPrev;
	private JButton buttonNext;
	private JButton buttonPlay;
	private JButton buttonStop;
	private ComboConstraints comboConstraints;
	private JTextField textConnectMass1;
	private JTextField textConnectMass2;
	private JButton buttonConnectMasses;
	
	public PropertiesWindow(ResourceManager rm) {
		this.setTitle("Properties");
		this.setSize(300,300);
		this.setLocation(720, 20);
		this.rm = rm;
		this.model = rm.getStringModel();
		
		panel = new JPanel();
		panel.setLayout(new GridLayout(8,2));

		panel.add(new JLabel("Mass No"));
		textMassNo = new JTextField("");
		textMassNo.addActionListener(this);
		panel.add(textMassNo);
		
		panel.add(new JLabel("Mass [kg]"));
		textMassAmount = new JTextField("");
		textMassAmount.addActionListener(this);
		panel.add(textMassAmount);
		
		panel.add(new JLabel("Friction [N sec/m]"));
		textFriction = new JTextField("");
		textFriction.addActionListener(this);
		panel.add(textFriction);
	
		panel.add(new JLabel("Constraints: "));
		comboConstraints = rm.getComboConstraints();
		panel.add(comboConstraints);
		
		buttonPrev = new JButton("Prev");
		buttonPrev.addActionListener(this);
		panel.add(buttonPrev);
		buttonNext = new JButton("Next");
		buttonNext.addActionListener(this);
		panel.add(buttonNext);
		
		buttonPlay = new JButton("||");
		buttonPlay.addActionListener(this);
		panel.add(buttonPlay);
		
		buttonStop = new JButton("Stop Mass");
		buttonStop.addActionListener(this);
		panel.add(buttonStop);
		
		textConnectMass1 = new JTextField("");
		panel.add(textConnectMass1);
		textConnectMass2 = new JTextField("");
		panel.add(textConnectMass2);
		buttonConnectMasses = new JButton("connect");
		buttonConnectMasses.addActionListener(this);
		panel.add(buttonConnectMasses);
		panel.add( new JLabel("m1 and m2"));
		
		this.getContentPane().add(panel);
		this.pack();
		
	}
	
	public void selectMass(Mass m, int massNo) {
		if(m != null) {
			textMassAmount.setText(""+m.getM());
			textFriction.setText(""+m.getB());
			textMassNo.setText(""+massNo);
		} else {
			textMassAmount.setText("");
			textFriction.setText("");
			textMassNo.setText("");
		}		
	}

	public void actionPerformed(ActionEvent ae) {
		Mass m = rm.getSelectedMass();
		
		/* mass no girilmis ise */
		if ( ae.getSource() == textMassNo ) {
			int massNo = Integer.parseInt(textMassNo.getText());
			if( massNo >= 0 && massNo < model.getNumberOfMasses() ) {
				Mass mass = model.getMass( massNo );
				rm.setSelectedMass( mass, massNo );
			}
		}
		
		/* kutle yahut surtunme girilmis ise */
		if( m != null ) {
			if( ae.getSource() == textMassAmount ) {
				double massAmount = Double.parseDouble(textMassAmount.getText()); 
				m.setM( massAmount );
				rm.getDisplay().repaint();
			} else if ( ae.getSource() == textFriction ) {
				double friction = Double.parseDouble(textFriction.getText());
				m.setB( friction );
			}
			if ( ae.getSource() == buttonStop) {
				m.setV(new Vector2D(0.0, 0.0));
			}
		}
		
		/* sonraki yahut onceki butonuna basilmis ise */		
		if ( ae.getSource() == buttonNext ) {
			if( m != null ) {
				int massNo = Integer.parseInt(textMassNo.getText()) + 1;
				if( massNo < model.getNumberOfMasses() ) {
					Mass mass = model.getMass( massNo );
					rm.setSelectedMass( mass, massNo );
				}
			} else {
				rm.setSelectedMass( model.getMass(0), 0);
			}
			rm.getDisplay().repaint();
		}
		
		if ( ae.getSource() == buttonPrev ) {
			if( m != null ) {
				int massNo = Integer.parseInt(textMassNo.getText()) - 1;
				if( massNo >= 0 ) {
					Mass mass = model.getMass( massNo );
					rm.setSelectedMass( mass, massNo );
				}
			} else {
				rm.setSelectedMass( model.getMass(model.getNumberOfMasses()-1), model.getNumberOfMasses()-1 );
			}
			rm.getDisplay().repaint();
		}
		
		/* Play/Pause tusuna basilmis ise */
		if ( ae.getSource() == buttonPlay ) {
			if( buttonPlay.getText()=="||" ) {
				buttonPlay.setText(">");
				rm.setPaused(true);
			} else {
				buttonPlay.setText("||");
				rm.setPaused(false);
			}
		}
		
		if( ae.getSource() == buttonConnectMasses ) {
			int massNo1 = Integer.parseInt( textConnectMass1.getText() );
			int massNo2 = Integer.parseInt( textConnectMass2.getText() );
			Mass mass1 = model.getMass(massNo1);
			Mass mass2 = model.getMass(massNo2);
			model.addSpring( new Spring(mass1, mass2) );
		}
	}
	
}
